<div class="p-3 border-x">
    <span class="layui-btn layui-btn-sm add-contract">+ 添加合同</span>
</div>
<div>
    <table class="layui-hide" id="contract" lay-filter="contract"></table>
</div>

<script>
function initContract(){
    if($('#partyTab').find('li').eq(1).data('load') == 'true'){
        return false;
    }
    $('#partyTab').find('li').eq(1).data('load','true');

    layui.use(['tool', 'table', 'oaPicker'], function(){
        let tool = layui.tool, table = layui.table, oaPicker = layui.oaPicker;
    
    layui.contractTable = table.render({
        elem: '#contract',
        title: '合同列表',
        cellMinWidth:80,
        url: '/contract/api/get_party_contracts',
        where: { party_id: $('#party_id').val() },
        page: true,
        limit: 10,
        cols: [[
            {field: 'contract_name', title: '合同名称', width: 180, align:'center'},
            {field: 'contract_number', title: '合同编号'},
            {field: 'amount', title: '金额', templet: function(d){
                return '¥' + (d.amount || 0).toFixed(2);}},
            {field: 'update_time', title: '更新时间', align: 'center', templet: function(d){
                return layui.util.toDateString(d.update_time * 1000, 'yyyy-MM-dd HH:mm:ss');}},
            {fixed: 'right', width: 120, title: '操作', align:'center', templet: function(d){
                return '<div class="layui-btn-group">'+
                    '<a class="layui-btn layui-btn-xs" lay-event="view">查看</a>'+
                    '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">移除</a>'+
                '</div>';
            }}
        ]]
    });
        
        $('body').on('click','.add-contract',function(){
            let opts = {
                title: '选择合同',
                url: '/contract/api/get_all_contracts',
                area: ['600px', '580px'],
                cols: [
                    {field: 'contract_name', title: '合同名称', width: 150},
                    {field: 'contract_number', title: '合同编号'},
                    {field: 'amount', title: '金额'}
                ],
                valueField: 'contract_id',
                page: true,
                search: true
            };
            
            oaPicker.picker(opts, 1, function(selectData){
                if(selectData.length > 0) {
                    let callback = function(e) {
                        layer.msg(e.msg);
                        if(e.code === 0) {
                            layui.contractTable.reload();
                        }
                    };
                    if (!selectData[0] || !selectData[0].contract_id) {
                        layer.msg('请选择有效的合同');
                        return;
                    }
                    tool.post("/contract/api/addPartyContract", {
                        party_id: $('#party_id').val(),
                        contract_id: selectData[0].contract_id,
                        contract_name: selectData[0].contract_name,
                        contract_number: selectData[0].contract_number
                    }, callback);
                }
            });
        });
        
        table.on('tool(contract)', function(obj){
            var data = obj.data;
            if(obj.event === 'view') {
                tool.side('/contract/contract/view?id=' + data.contract_id, '查看合同');
            } else if(obj.event === 'delete') {
                layer.confirm('确认移除该合同？', {icon: 3, title:'提示'}, function(index){
                    let callback = function(e) {
                        layer.msg(e.msg);
                        if(e.code === 0) {
                            layui.contractTable.reload();
                        }
                    };
                    tool.delete("/contract/api/delPartyContract", {
                        party_id: $('#party_id').val(),
                        contract_id: data.contract_id
                    }, callback);
                    layer.close(index);
                });
            }
        });
    });
}

</script>